# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: server\http_responses\http_errors.py
from typing import List, Union, Dict
import math, datetime
from dateutil.tz import tzlocal
from helpers.http_utils import http_date
from server.http_responses.json_serializable import JsonSerializableResponse


class ApiErrors:
    FEATURE_NOT_ALLOWED = 1
    MAX_CHILD_USERS_REACHED = 2
    LICENSE_NOT_ACTIVATED = 3
    LICENSE_EXPIRED = 4
    LICENSE_POC_EXPIRED = 6
    INVALID_FILTER = 16
    DB_CONNECTION_ERROR = 32
    NO_TARGET_FOR_ALL_REPORT = 48
    ONLY_DEMO_TARGETS_ALLOWED = 49
    MI_NOT_FROM_LOCALHOST = 64
    MI_NO_SESSION_ID = 65
    MI_RECURRENT_SCANS_NOT_ALLOWED = 66
    MI_ONLY_SCAN_NOW_ALLOWED = 67
    MI_SCAN_NOW_NOT_POSSIBLE_TARGET_EXCLUDED_HOURS = 67
    MI_SCAN_NOW_NOT_POSSIBLE_SYSTEM_EXCLUDED_HOURS = 68
    MI_CONTINUOUS_NOT_MODE_SUPPORTED = 69
    SA_NO_MORE_WORKER_SLOTS = 257
    SA_DUPLICATE_JOB_ID = 258
    SA_JOB_NOT_FOUND = 259
    SA_WORKER_TERMINATED = 260
    TOO_MANY_REQUESTS = 429
    OTP_REQUIRED_BUT_NOT_SET = 512
    OTP_REQUIRED_BUT_NOT_SENT = 513
    PASSWORD_EXPIRED = 515
    PASSWORD_REUSE_ERROR = 516
    ACCOUNT_LOCKED = 517
    ACCOUNT_HAS_NO_PASSWORD = 518
    ACCOUNT_REJECTED = 519
    ACCOUNT_DISABLED = 520
    MULTIPLE_SESSIONS_NOT_SUPPORTED = 521
    PAUSE_SCAN_IS_NOT_RUNNING = 768
    PAUSE_INVALID_SCAN_STATUS = 769
    PAUSE_CANT_PAUSE_APP_TYPE = 770
    CANT_DELETE_TARGET = 1024


class HttpError(JsonSerializableResponse, Exception):
    def __init__(
        self,
        http_error,
        message=None,
        code=0,
        details=None,
        headers=None,
        cookie=None,
        disable_cache=True,
    ):
        super().__init__(
            dict(code=code, message=message, details=details),
            headers,
            http_error,
            cookie,
            disable_cache,
        )


class Http400(HttpError):
    def __init__(
        self,
        message="Bad Request",
        code=400,
        details=None,
        headers=None,
        cookie=None,
        disable_cache=True,
    ):
        super().__init__(
            "400 Bad Request", message, code, details, headers, cookie, disable_cache
        )


BadRequest = Http400
BadApiRequest = Http400


class InvalidFilter(HttpError):
    def __init__(
        self,
        filter_name,
        error_message,
        url_parameter_name="q",
        headers=None,
        cookie=None,
        disable_cache=True,
    ):
        details = [
            {
                url_parameter_name: dict(
                    problems=[
                        {
                            "error_message:": error_message,
                            "param_path": url_parameter_name + "." + filter_name,
                            "code": "invalid_filter",
                        }
                    ],
                    src="url",
                )
            }
        ]
        super().__init__(
            "400 Bad Request",
            "Validation errors",
            ApiErrors.INVALID_FILTER,
            details,
            headers,
            cookie,
            disable_cache,
        )


class Http401(HttpError):
    def __init__(
        self,
        message="Unauthorized",
        code=401,
        details=None,
        headers=None,
        cookie=None,
        disable_cache=True,
    ):
        super().__init__(
            "401 Unauthorized", message, code, details, headers, cookie, disable_cache
        )


InvalidAuth = Http401
InvalidSession = Http401
SessionExpired = Http401


class Http403(HttpError):
    def __init__(
        self,
        message="Forbidden",
        code=403,
        details=None,
        headers=None,
        cookie=None,
        disable_cache=True,
    ):
        super().__init__(
            "403 Forbidden", message, code, details, headers, cookie, disable_cache
        )


Forbidden = Http403


class Http404(HttpError):
    def __init__(
        self,
        message="Not Found",
        code=404,
        details=None,
        headers=None,
        cookie=None,
        disable_cache=True,
    ):
        super().__init__(
            "404 Not Found", message, code, details, headers, cookie, disable_cache
        )


RouteNotFound = Http404
ClassNotFound = Http404
MethodNotFound = Http404


class Http409(HttpError):
    def __init__(
        self,
        message="Conflict",
        code=409,
        details=None,
        headers=None,
        cookie=None,
        disable_cache=True,
    ):
        super().__init__(
            "409 Conflict", message, code, details, headers, cookie, disable_cache
        )


class TooManyRequests(HttpError):
    def __init__(self, retry_after, headers=None, cookie=None, disable_cache=True):
        retry_after = math.ceil(retry_after) + 1
        date = http_date(
            datetime.datetime.now(tzlocal()) + (datetime.timedelta(seconds=retry_after))
        )
        if headers is None:
            headers = {"Retry-After": date}
        else:
            headers["Retry-After"] = date
        details = dict(retry_after=date, delay_seconds=retry_after)
        super().__init__(
            "429 Too Many Requests",
            "Too Many Requests",
            429,
            details,
            headers,
            cookie,
            disable_cache,
        )


class Http500(HttpError):
    def __init__(
        self,
        message="Internal Server Error",
        code=500,
        details=None,
        headers=None,
        cookie=None,
        disable_cache=True,
    ):
        super().__init__(
            "500 Internal Server Error",
            message,
            code,
            details,
            headers,
            cookie,
            disable_cache,
        )
